#include <QDebug>
#include <QMessageBox>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "version.h"
#include "../firmware/workspace_sri_v1/SRI_v1/include/status.h"

void MainWindow::actionSerSRIPortTriggered() {
  dlgSelectPort->refreshPorts();
  dlgSelectPort->show();
}

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow) {
  ui->setupUi(this);

  ui->tabWidget->setCurrentIndex(0);

  dlgSelectPort = new DialogSelectPort();
  dlgSelectPort->hide();

  QString str;
  str.append("Smart Radio Interface - ");
  str.append(SOFTWARE_VERSION);

  this->setWindowTitle(str);

  settings = new Settings();
  status = new Status();

  serialPort = new CommClass();

  timerPollStatus = new QTimer(this);
  timerPingPongEvent = new QTimer(this);

  QString freqVFOA = QString("14.195.00");
  ui->lcdNumberFreqVFOA->display(freqVFOA);

  QString freqVFOB = QString("14.025.34");
  ui->lcdNumberFreqVFOB->display(freqVFOB);

  ui->labelVFOATX->setStyleSheet(QLABEL_TX_STYLESHEET);
  ui->labelVFOARX->setStyleSheet(QLABEL_RX_STYLESHEET);

  settings->setSerialPtr(serialPort);
  status->setSerialPtr(serialPort);

  connect(timerPollStatus, SIGNAL(timeout()), this, SLOT(timerPollStatusUpdate()));
  connect(timerPingPongEvent, SIGNAL(timeout()), this, SLOT(timerPingPongEventUpdate()));
  connect(ui->actionSetSRIPort,SIGNAL(triggered()),this,SLOT(actionSerSRIPortTriggered()));

  connect(ui->checkBoxPTTInputFootswitch,SIGNAL(clicked()),this,SLOT(actionPTTInputClicked()));
  connect(ui->checkBoxPTTInputFSKCWRTS,SIGNAL(clicked()),this,SLOT(actionPTTInputClicked()));
  connect(ui->checkBoxPTTInputVOXSense,SIGNAL(clicked()),this,SLOT(actionPTTInputClicked()));
  connect(ui->checkBoxPTTInputWinKeyDTR,SIGNAL(clicked()),this,SLOT(actionPTTInputClicked()));
  connect(ui->checkBoxPTTInputWinKeyRTS,SIGNAL(clicked()),this,SLOT(actionPTTInputClicked()));

  connect(ui->checkBoxPTTInputFSKCWRTSInvert,SIGNAL(clicked()),this,SLOT(actionPTTInputInvertClicked()));
  connect(ui->checkBoxPTTInputWinKeyDTRInvert,SIGNAL(clicked()),this,SLOT(actionPTTInputInvertClicked()));
  connect(ui->checkBoxPTTInputWinKeyRTSInvert,SIGNAL(clicked()),this,SLOT(actionPTTInputInvertClicked()));
  connect(ui->checkBoxPTTInputVOXInvert,SIGNAL(clicked()),this,SLOT(actionPTTInputInvertClicked()));
}

MainWindow::~MainWindow() {
  if (serialPort->isOpen())
    serialPort->closePort();

  delete ui;
}

CommClass* MainWindow::getSerialPtr() {
  return(serialPort);
}

void MainWindow::updateUIfromStatus() {
  if (settings->getPTTInputEnabled(SETTING_PTT_INPUT_FSKCW_RTS)) {
    if (status->getPTTInputState(STATUS_PTT_INPUT_FSKCW_RTS))
      ui->labelPTTInputIndicatorFSKCWRTS->setStyleSheet(QLABEL_TX_STYLESHEET);
    else
      ui->labelPTTInputIndicatorFSKCWRTS->setStyleSheet(QLABEL_RX_STYLESHEET);
  }
  else
    ui->labelPTTInputIndicatorFSKCWRTS->setStyleSheet(QLABEL_TXRXNONE_STYLESHEET);

  if (settings->getPTTInputEnabled(SETTING_PTT_INPUT_FOOTSWITCH)) {
    if (status->getPTTInputState(STATUS_PTT_INPUT_FOOTSWITCH))
      ui->labelPTTInputIndicatorFootswitch->setStyleSheet(QLABEL_TX_STYLESHEET);
    else
      ui->labelPTTInputIndicatorFootswitch->setStyleSheet(QLABEL_RX_STYLESHEET);
  }
  else
    ui->labelPTTInputIndicatorFootswitch->setStyleSheet(QLABEL_TXRXNONE_STYLESHEET);

  if (settings->getPTTInputEnabled(SETTING_PTT_INPUT_VOX_SENSE)) {
    if (status->getPTTInputState(STATUS_PTT_INPUT_VOX_SENSE))
      ui->labelPTTInputIndicatorVOXSense->setStyleSheet(QLABEL_TX_STYLESHEET);
    else
      ui->labelPTTInputIndicatorVOXSense->setStyleSheet(QLABEL_RX_STYLESHEET);
  }
  else
    ui->labelPTTInputIndicatorVOXSense->setStyleSheet(QLABEL_TXRXNONE_STYLESHEET);

  if (settings->getPTTInputEnabled(SETTING_PTT_INPUT_WK_DTR)) {
    if (status->getPTTInputState(STATUS_PTT_INPUT_WK_DTR))
      ui->labelPTTInputIndicatorWinKeyDTR->setStyleSheet(QLABEL_TX_STYLESHEET);
    else
      ui->labelPTTInputIndicatorWinKeyDTR->setStyleSheet(QLABEL_RX_STYLESHEET);
  }
  else
    ui->labelPTTInputIndicatorWinKeyDTR->setStyleSheet(QLABEL_TXRXNONE_STYLESHEET);

  if (settings->getPTTInputEnabled(SETTING_PTT_INPUT_WK_RTS)) {
    if (status->getPTTInputState(STATUS_PTT_INPUT_WK_RTS))
      ui->labelPTTInputIndicatorWinKeyRTS->setStyleSheet(QLABEL_TX_STYLESHEET);
    else
      ui->labelPTTInputIndicatorWinKeyRTS->setStyleSheet(QLABEL_RX_STYLESHEET);
  }
  else
    ui->labelPTTInputIndicatorWinKeyRTS->setStyleSheet(QLABEL_TXRXNONE_STYLESHEET);

  ui->lcdNumberFreqVFOA->display(Misc::transformFreqToString(status->getVFOAFreq()));
}

void MainWindow::updateUIfromSettings() {
  bool state = true;

  //********* START RADIO SETTINGS *********//
  ui->groupBoxCATInterface->setChecked(settings->getRadioCATInterfaceEnabled());

  ui->comboBoxCATBaudrate->setCurrentIndex(Misc::transformBaudrateToIndex(settings->getRadioCATBaudrate()));
  ui->comboBoxCATParity->setCurrentIndex(settings->getRadioCATParity());
  ui->comboBoxCATStopbits->setCurrentIndex(settings->getRadioCATStopbits());
  ui->comboBoxRadioModel->setCurrentIndex(settings->getRadioModel());
  ui->spinBoxCATCIVAddress->setValue(settings->getRadioCATICOMCIV());
  ui->checkBoxCATFlowControl->setChecked(settings->getRadioCATFlowControl());
  ui->checkBoxCATJumperRTSCTS->setChecked(settings->getRadioCATJumperRTSCTS());

  ui->comboBoxCATRetrieveType->setCurrentIndex(settings->getRadioCATRetrieveType());
  ui->spinBoxCATPollingRate->setValue(settings->getRadioCATPollRate());
  //********* END RADIO SETTINGS *********//

  //********* START PTT SETTINGS *********//
  state = true;
  ui->checkBoxPTTInputFootswitch->blockSignals(state);
  ui->checkBoxPTTInputWinKeyRTS->blockSignals(state);
  ui->checkBoxPTTInputWinKeyDTR->blockSignals(state);
  ui->checkBoxPTTInputFSKCWRTS->blockSignals(state);
  ui->checkBoxPTTInputVOXSense->blockSignals(state);
  ui->checkBoxPTTInputWinKeyRTSInvert->blockSignals(state);
  ui->checkBoxPTTInputWinKeyDTRInvert->blockSignals(state);
  ui->checkBoxPTTInputFSKCWRTSInvert->blockSignals(state);
  ui->checkBoxPTTInputVOXInvert->blockSignals(state);

  ui->checkBoxPTTInputFootswitch->setChecked(Misc::transformPTTInputEnable(settings->getPTTInputEnabled(),SETTING_PTT_INPUT_FOOTSWITCH));
  ui->checkBoxPTTInputWinKeyRTS->setChecked(Misc::transformPTTInputEnable(settings->getPTTInputEnabled(),SETTING_PTT_INPUT_WK_RTS));
  ui->checkBoxPTTInputWinKeyDTR->setChecked(Misc::transformPTTInputEnable(settings->getPTTInputEnabled(),SETTING_PTT_INPUT_WK_DTR));
  ui->checkBoxPTTInputFSKCWRTS->setChecked(Misc::transformPTTInputEnable(settings->getPTTInputEnabled(),SETTING_PTT_INPUT_FSKCW_RTS));
  ui->checkBoxPTTInputVOXSense->setChecked(Misc::transformPTTInputEnable(settings->getPTTInputEnabled(),SETTING_PTT_INPUT_VOX_SENSE));

  ui->checkBoxPTTInputWinKeyRTSInvert->setChecked(Misc::transformPTTInputEnable(settings->getPTTInputInvert(),SETTING_PTT_INPUT_WK_RTS));
  ui->checkBoxPTTInputWinKeyDTRInvert->setChecked(Misc::transformPTTInputEnable(settings->getPTTInputInvert(),SETTING_PTT_INPUT_WK_DTR));
  ui->checkBoxPTTInputFSKCWRTSInvert->setChecked(Misc::transformPTTInputEnable(settings->getPTTInputInvert(),SETTING_PTT_INPUT_FSKCW_RTS));
  ui->checkBoxPTTInputVOXInvert->setChecked(Misc::transformPTTInputEnable(settings->getPTTInputInvert(),SETTING_PTT_INPUT_VOX_SENSE));

  state = false;
  ui->checkBoxPTTInputFootswitch->blockSignals(state);
  ui->checkBoxPTTInputWinKeyRTS->blockSignals(state);
  ui->checkBoxPTTInputWinKeyDTR->blockSignals(state);
  ui->checkBoxPTTInputFSKCWRTS->blockSignals(state);
  ui->checkBoxPTTInputVOXSense->blockSignals(state);
  ui->checkBoxPTTInputWinKeyRTSInvert->blockSignals(state);
  ui->checkBoxPTTInputWinKeyDTRInvert->blockSignals(state);
  ui->checkBoxPTTInputFSKCWRTSInvert->blockSignals(state);
  ui->checkBoxPTTInputVOXInvert->blockSignals(state);

  state = true;
  ui->comboBoxPTTOutputPhone->blockSignals(state);
  ui->comboBoxPTTOutputCW->blockSignals(state);
  ui->comboBoxPTTOutputDigital->blockSignals(state);
  ui->checkBoxPTTOutputAmpflier->blockSignals(state);
  ui->checkBoxPTTOutputInhibit->blockSignals(state);
  ui->spinBoxPTTAmplifierPostDelay->blockSignals(state);
  ui->spinBoxPTTAmplifierPostDelay->blockSignals(state);
  ui->spinBoxPTTAmplifierPreDelay->blockSignals(state);
  ui->spinBoxPTTAntennaPostDelay->blockSignals(state);
  ui->spinBoxPTTAntennaPreDelay->blockSignals(state);
  ui->spinBoxPTTInhibitPostDelay->blockSignals(state);
  ui->spinBoxPTTInhibitPreDelay->blockSignals(state);
  ui->spinBoxPTTRadioPostDelay->blockSignals(state);
  ui->spinBoxPTTRadioPreDelay->blockSignals(state);

  ui->comboBoxPTTOutputPhone->setCurrentIndex(settings->getPTTOutputPhoneConf());
  ui->comboBoxPTTOutputCW->setCurrentIndex(settings->getPTTOutputCWConf());
  ui->comboBoxPTTOutputDigital->setCurrentIndex(settings->getPTTOutputDigitalConf());
  ui->checkBoxPTTOutputAmpflier->setChecked(settings->getPTTOutputAmplifierEnabled());
  ui->checkBoxPTTOutputInhibit->setChecked(settings->getPTTOutputInhibitEnabled());
  ui->spinBoxPTTAmplifierPostDelay->setValue(settings->getPTTAmpPostDelay());
  ui->spinBoxPTTAmplifierPreDelay->setValue(settings->getPTTAmpPreDelay());
  ui->spinBoxPTTAntennaPostDelay->setValue(settings->getPTTAntennaPostDelay());
  ui->spinBoxPTTAntennaPreDelay->setValue(settings->getPTTAntennaPreDelay());
  ui->spinBoxPTTInhibitPostDelay->setValue(settings->getPTTInhibitPostDelay());
  ui->spinBoxPTTInhibitPreDelay->setValue(settings->getPTTInhibitPreDelay());
  ui->spinBoxPTTRadioPostDelay->setValue(settings->getPTTRadioPostDelay());
  ui->spinBoxPTTRadioPreDelay->setValue(settings->getPTTRadioPreDelay());


  state = false;
  ui->comboBoxPTTOutputPhone->blockSignals(state);
  ui->comboBoxPTTOutputCW->blockSignals(state);
  ui->comboBoxPTTOutputDigital->blockSignals(state);
  ui->checkBoxPTTOutputAmpflier->blockSignals(state);
  ui->checkBoxPTTOutputInhibit->blockSignals(state);
  ui->spinBoxPTTAmplifierPostDelay->blockSignals(state);
  ui->spinBoxPTTAmplifierPostDelay->blockSignals(state);
  ui->spinBoxPTTAmplifierPreDelay->blockSignals(state);
  ui->spinBoxPTTAntennaPostDelay->blockSignals(state);
  ui->spinBoxPTTAntennaPreDelay->blockSignals(state);
  ui->spinBoxPTTInhibitPostDelay->blockSignals(state);
  ui->spinBoxPTTInhibitPreDelay->blockSignals(state);
  ui->spinBoxPTTRadioPostDelay->blockSignals(state);
  ui->spinBoxPTTRadioPreDelay->blockSignals(state);
  //********* END PTT SETTINGS *********//
}

void MainWindow::msgBoxClosed(QAbstractButton* button) {

}


void MainWindow::showMessageBox(QString title, QString text, enum QMessageBox::Icon icon) {
   QMessageBox* msgBox = new QMessageBox(this);
   msgBox->setIcon(icon);
   msgBox->setAttribute(Qt::WA_DeleteOnClose);
   msgBox->setStandardButtons(QMessageBox::Ok);
   msgBox->setWindowTitle(title);
   msgBox->setText(text);
   msgBox->setModal(false);
   msgBox->open(this, SLOT(msgBoxClosed(QAbstractButton*)));
}

void MainWindow::parseCommData(StructMessage message) {
  if (message.cmd == SRI_CMD_CONNECT) {
    QString msg;
    msg.append("Connected to: ");
    msg.append(currentSRIPort);
    msg.append(" - Firmware rev: ");

    for (unsigned char i=0;i<message.length;i++)
      msg.append(message.data[i]);

    statusBar()->showMessage(msg, 5000);

    lastPongCounter = 0;
  }
  else if (message.cmd == SRI_CMD_SEND_SETTINGS) {
    //qDebug() << "LENGTH: " << message.length;
    //qDebug() << "Offset: " << (quint16)((message.data[0] << 8) + message.data[1]);
    settings->newDataToStruct(message.data+2, (quint16)((message.data[0] << 8) + message.data[1]), message.length-2);

    lastPongCounter = 0;
  }
  else if (message.cmd == SRI_CMD_GET_FIRMWARE_REV) {
    QString msg;
    msg.append("openSRI current\nfirmware version: ");

    for (unsigned char i=0;i<message.length;i++)
      msg.append(message.data[i]);

    showMessageBox("Firmware version",msg,QMessageBox::Information);

    lastPongCounter = 0;
  }
  else if (message.cmd == SRI_CMD_UPDATE_UI) {
    //qDebug("UPDATE UI");
    updateUIfromSettings();

    lastPongCounter = 0;
  }
  else if (message.cmd == SRI_CMD_SEND_STATUS) {
    status->newDataToStruct(message.data+2, (quint16)((message.data[0] << 8) + message.data[1]), message.length-2);

    updateUIfromStatus();

    lastPongCounter = 0;
  }
  else if (message.cmd == SRI_CMD_SEND_PING) {
    if (serialPort->isOpen()) {
      //Send a PONG back to the SRI interface to know we are alive
      serialPort->addTXMessage(SRI_CMD_SEND_PONG);
      //qDebug("PING CMD");
      lastPongCounter = 0;
    }
  }
  else if (message.cmd == SRI_CMD_SEND_PONG) {
    //qDebug("PONG CMD");
    lastPongCounter = 0;
  }
}

void MainWindow::timerPollStatusUpdate() {
  serialPort->processData();

  if (serialPort->getRXQueueSize()) {
    parseCommData(serialPort->getRXQueueFirst());
  }
}

void MainWindow::timerPingPongEventUpdate() {
  if (serialPort->isOpen()) {
    serialPort->addTXMessage(SRI_CMD_SEND_PING);

    lastPongCounter++;

    if (lastPongCounter >= SRI_PING_TIMEOUT_LIMIT) {
      lastPongCounter = 0;
      on_actionDisconnect_triggered();

      showMessageBox("Warning","The SRI interface is not responding",QMessageBox::Warning);

    }
  }
}

void MainWindow::on_actionConnect_triggered() {
  currentSRIPort = dlgSelectPort->getPortName();

  if (currentSRIPort != "Undefined") {
    if (serialPort->openPort(currentSRIPort,57600)) {
      ui->actionConnect->setEnabled(false);
      ui->actionDisconnect->setEnabled(true);

      timerPollStatus->setInterval(TIMER_POLL_STATUS_INTERVAL);
      timerPollStatus->start();

      timerPingPongEvent->setInterval(SRI_PING_INTERVAL);
      timerPingPongEvent->start();

      serialPort->addTXMessage(SRI_CMD_CONNECT);
    }
  }
  else {
    QMessageBox::warning(this, tr("Warning"),tr("No port has been defined to which\nthe SRI interface is connected to"));

    dlgSelectPort->refreshPorts();
    dlgSelectPort->show();
  }
}

void MainWindow::disconnectSerialPort() {
  if (serialPort->isOpen()) {
    timerPollStatus->stop();

    ui->actionConnect->setEnabled(true);
    ui->actionDisconnect->setEnabled(false);

    QString msg;
    msg.append("Disconnected from: ");
    msg.append(currentSRIPort);
    statusBar()->showMessage(msg, 5000);

    serialPort->closePort();
  }
}

void MainWindow::on_actionDisconnect_triggered() {
  if (serialPort->isOpen()) {
    serialPort->addTXMessage(SRI_CMD_DISCONNECT);

    //Wait two seconds before we disconnect the serial port, so that
    //the message has time to be sent to the uC
    QTimer::singleShot(1000, this, SLOT(disconnectSerialPort()));
  }
}

void MainWindow::on_actionQuit_triggered() {
  if (serialPort->isOpen())
    serialPort->closePort();

  exit(0);
}

void MainWindow::on_actionTestButton_triggered() {
  serialPort->addTXMessage(SRI_CMD_TEST_COMMAND);
}

void MainWindow::on_pushButtonCATSettingsApply_clicked() {
  quint32 baudrate = 0;

  baudrate = Misc::transformIndexToBaudrate(ui->comboBoxCATBaudrate->currentIndex());

  if (ui->groupBoxCATInterface->isChecked())
    settings->setRadioCATInterfaceEnabled(1);
  else
    settings->setRadioCATInterfaceEnabled(0);

  settings->setRadioCATBaudrate(baudrate);
  settings->setRadioModel(ui->comboBoxRadioModel->currentIndex());
  settings->setRadioCATParity(ui->comboBoxCATParity->currentIndex());
  settings->setRadioCATStopbits(ui->comboBoxCATStopbits->currentIndex());

  if (ui->checkBoxCATJumperRTSCTS->isChecked())
    settings->setRadioCATJumperRTSCTS(1);
  else
    settings->setRadioCATJumperRTSCTS(0);

  if (ui->checkBoxCATFlowControl->isChecked())
    settings->setRadioCATFlowControl(1);
  else
    settings->setRadioCATFlowControl(0);

  settings->setRadioCATICOMCIV(ui->spinBoxCATCIVAddress->value());

  settings->setRadioCATRetrieveType(ui->comboBoxCATRetrieveType->currentIndex());
  settings->setRadioCATPollRate(ui->spinBoxCATPollingRate->value());

  serialPort->addTXMessage(SRI_CMD_CAT_SETTINGS_CHANGED);
}

void MainWindow::on_actionGetVersion_triggered() {
  serialPort->addTXMessage(SRI_CMD_GET_FIRMWARE_REV);
}

void MainWindow::actionPTTInputClicked() {
  quint8 state = 0;

  if (ui->checkBoxPTTInputFootswitch->isChecked())
    state |= (1<<SETTING_PTT_INPUT_FOOTSWITCH);
  if (ui->checkBoxPTTInputFSKCWRTS->isChecked())
    state |= (1<<SETTING_PTT_INPUT_FSKCW_RTS);
  if (ui->checkBoxPTTInputVOXSense->isChecked())
    state |= (1<<SETTING_PTT_INPUT_VOX_SENSE);
  if (ui->checkBoxPTTInputWinKeyDTR->isChecked())
    state |= (1<<SETTING_PTT_INPUT_WK_DTR);
  if (ui->checkBoxPTTInputWinKeyRTS->isChecked())
    state |= (1<<SETTING_PTT_INPUT_WK_RTS);

  settings->setPTTInputEnabled(state);
}

void MainWindow::actionPTTInputInvertClicked() {
  quint8 state = 0;

  if (ui->checkBoxPTTInputFSKCWRTSInvert->isChecked())
    state |= (1<<SETTING_PTT_INPUT_FSKCW_RTS);
  if (ui->checkBoxPTTInputVOXInvert->isChecked())
    state |= (1<<SETTING_PTT_INPUT_VOX_SENSE);
  if (ui->checkBoxPTTInputWinKeyDTRInvert->isChecked())
    state |= (1<<SETTING_PTT_INPUT_WK_DTR);
  if (ui->checkBoxPTTInputWinKeyRTSInvert->isChecked())
    state |= (1<<SETTING_PTT_INPUT_WK_RTS);

  settings->setPTTInputInvert(state);
}

void MainWindow::on_comboBoxPTTOutputPhone_currentIndexChanged(int index) {
    settings->setPTTOutputPhoneConf(index);
}

void MainWindow::on_comboBoxPTTOutputCW_currentIndexChanged(int index) {
  settings->setPTTOutputCWConf(index);
}


void MainWindow::on_comboBoxPTTOutputDigital_currentIndexChanged(int index){
  settings->setPTTOutputDigitalConf(index);
}

void MainWindow::on_checkBoxPTTOutputInhibit_toggled(bool checked) {
  if (checked)
    settings->setPTTOutputInhibitEnabled(1);
  else
    settings->setPTTOutputInhibitEnabled(0);
}

void MainWindow::on_checkBoxPTTOutputAmpflier_toggled(bool checked) {
  if (checked)
    settings->setPTTOutputAmplifierEnabled(1);
  else
    settings->setPTTOutputAmplifierEnabled(0);
}

void MainWindow::on_spinBoxPTTRadioPreDelay_valueChanged(int arg1) {
  settings->setPTTRadioPreDelay(arg1);
}

void MainWindow::on_spinBoxPTTRadioPostDelay_valueChanged(int arg1) {
  settings->setPTTRadioPostDelay(arg1);
}

void MainWindow::on_spinBoxPTTAmplifierPreDelay_valueChanged(int arg1) {
  settings->setPTTAmpPreDelay(arg1);
}

void MainWindow::on_spinBoxPTTAmplifierPostDelay_valueChanged(int arg1) {
  settings->setPTTAmpPostDelay(arg1);
}

void MainWindow::on_spinBoxPTTInhibitPreDelay_valueChanged(int arg1) {
  settings->setPTTInhibitPreDelay(arg1);
}

void MainWindow::on_spinBoxPTTInhibitPostDelay_valueChanged(int arg1) {
  settings->setPTTInhibitPostDelay(arg1);
}

void MainWindow::on_spinBoxPTTAntennaPreDelay_valueChanged(int arg1) {
  settings->setPTTAntennaPreDelay(arg1);
}

void MainWindow::on_spinBoxPTTAntennaPostDelay_valueChanged(int arg1) {
  settings->setPTTAntennaPostDelay(arg1);
}

void MainWindow::on_actionSaveToEEPROM_triggered() {
  settings->saveToEEPROM();
}

void MainWindow::on_comboBoxAudioRoutingScenario_currentIndexChanged(int index) {
  bool blockState = true;
  ui->comboBoxAudioRoutingLineInCW->blockSignals(blockState);
  ui->comboBoxAudioRoutingLineInPhone->blockSignals(blockState);
  ui->comboBoxAudioRoutingLineInDigital->blockSignals(blockState);
  ui->comboBoxAudioRoutingMicPath->blockSignals(blockState);
  ui->comboBoxAudioRoutingCompGeneratedPhone->blockSignals(blockState);
  ui->comboBoxAudioRoutingCompGeneratedDigital->blockSignals(blockState);
  ui->checkBoxAudioRoutingMuteMic->blockSignals(blockState);
  ui->checkBoxAudioRoutingPreamp->blockSignals(blockState);
  ui->checkBoxAudioRoutingMuteLineOutPhone->blockSignals(blockState);
  ui->checkBoxAudioRoutingMuteLineOutDigital->blockSignals(blockState);

  ui->comboBoxAudioRoutingLineInCW->setCurrentIndex(settings->getAudioRoutingLineInSourceCW(index));
  ui->comboBoxAudioRoutingLineInPhone->setCurrentIndex(settings->getAudioRoutingLineInSourcePhone(index));
  ui->comboBoxAudioRoutingLineInDigital->setCurrentIndex(settings->getAudioRoutingLineInSourceDigital(index));
  ui->comboBoxAudioRoutingMicPath->setCurrentIndex(settings->getAudioRoutingMicPath(index));
  ui->comboBoxAudioRoutingCompGeneratedPhone->setCurrentIndex(settings->getAudioRoutingComputerGeneratedPathPhone(index));
  ui->comboBoxAudioRoutingCompGeneratedDigital->setCurrentIndex(settings->getAudioRoutingComputerGeneratedPathDigital(index));
  ui->checkBoxAudioRoutingMuteMic->setChecked(settings->getAudioRoutingMicMute(index));
  ui->checkBoxAudioRoutingPreamp->setChecked(settings->getAudioRoutingMicPreamp(index));
  ui->checkBoxAudioRoutingMuteLineOutPhone->setChecked(settings->getAudioRoutingMuteLineOutPhone(index));
  ui->checkBoxAudioRoutingMuteLineOutDigital->setChecked(settings->getAudioRoutingMuteLineOutDigital(index));

  blockState = false;
  ui->comboBoxAudioRoutingLineInCW->blockSignals(blockState);
  ui->comboBoxAudioRoutingLineInPhone->blockSignals(blockState);
  ui->comboBoxAudioRoutingLineInDigital->blockSignals(blockState);
  ui->comboBoxAudioRoutingMicPath->blockSignals(blockState);
  ui->comboBoxAudioRoutingCompGeneratedPhone->blockSignals(blockState);
  ui->comboBoxAudioRoutingCompGeneratedDigital->blockSignals(blockState);
  ui->checkBoxAudioRoutingMuteMic->blockSignals(blockState);
  ui->checkBoxAudioRoutingPreamp->blockSignals(blockState);
  ui->checkBoxAudioRoutingMuteLineOutPhone->blockSignals(blockState);
  ui->checkBoxAudioRoutingMuteLineOutDigital->blockSignals(blockState);
}

void MainWindow::on_comboBoxAudioRoutingLineInCW_currentIndexChanged(int index) {
  settings->setAudioRoutingLineInSourceCW(ui->comboBoxAudioRoutingScenario->currentIndex(),index);
}

void MainWindow::on_comboBoxAudioRoutingLineInPhone_currentIndexChanged(int index) {
  settings->setAudioRoutingLineInSourcePhone(ui->comboBoxAudioRoutingScenario->currentIndex(),index);
}


void MainWindow::on_comboBoxAudioRoutingLineInDigital_currentIndexChanged(int index) {
  settings->setAudioRoutingLineInSourceDigital(ui->comboBoxAudioRoutingScenario->currentIndex(),index);
}

void MainWindow::on_comboBoxAudioRoutingMicPath_currentIndexChanged(int index) {
  settings->setAudioRoutingMicPath(ui->comboBoxAudioRoutingScenario->currentIndex(),index);
}

void MainWindow::on_checkBoxAudioRoutingMuteMic_clicked(bool checked) {
  settings->setAudioRoutingMicMute(ui->comboBoxAudioRoutingScenario->currentIndex(),checked);
}


void MainWindow::on_checkBoxAudioRoutingPreamp_clicked(bool checked) {
  settings->setAudioRoutingMicPreamp(ui->comboBoxAudioRoutingScenario->currentIndex(),checked);
}

void MainWindow::on_checkBoxAudioRoutingMuteLineOutPhone_clicked(bool checked) {
  settings->setAudioRoutingMuteLineOutPhone(ui->comboBoxAudioRoutingScenario->currentIndex(),checked);
}

void MainWindow::on_checkBoxAudioRoutingMuteLineOutDigital_clicked(bool checked) {
  settings->setAudioRoutingMuteLineOutDigital(ui->comboBoxAudioRoutingScenario->currentIndex(),checked);
}


void MainWindow::on_comboBoxAudioRoutingCompGeneratedPhone_currentIndexChanged(int index) {
  settings->setAudioRoutingComputerGeneratedPathPhone(ui->comboBoxAudioRoutingScenario->currentIndex(),index);
}

void MainWindow::on_comboBoxAudioRoutingCompGeneratedDigital_currentIndexChanged(int index) {
  settings->setAudioRoutingComputerGeneratedPathDigital(ui->comboBoxAudioRoutingScenario->currentIndex(),index);
}
